#include "headfiles.h"

ActivePoint active_point;
int remainings = 0;
unordered_set<char> set;

bool createSuffixTree(char *str, SuffixNode * &root) {
    if (str == NULL) {
        root = NULL;
        return true;
    }

    string tmp_str(str);
    size_t str_size = tmp_str.size();
    size_t loc;

    // allocate the root node
    root = new SuffixNode;
    if (root == NULL) {
        cout << "Allocate root node error, NULL!" << endl;
        return false;
    }
    root->start_pos = -1;
    root->end_pos = -1;
    root->cur_pos = -1;
    active_point.node = root;
    active_point.edge = NULL;
    active_point.length = 0;
    remainings = 1;

    for (loc = 0; loc < str_size; loc++) {
        root->cur_pos = loc + 1;
        auto cit = set.find(tmp_str[loc]);
        // There is an edge starting from the current character
        // update the active point
        if (cit != set.end()) {
            active_point.
        }
        SuffixNode *tmp_node = new SuffixNode;
        tmp_node->start_pos = loc;
        tmp_node->parent = root;
        // First child
        if (root->children_first == NULL) {
            root->children_first = root->children_last = tmp_node;
        }
        else {
            root->children_last->next = tmp_node;
            root->children_last = tmp_node;
        }
    }
}

static void printString(char *str, int start, int end) {
    int i;
    for (i = start; i < end; i++) {
        printf("%c", str[i]);
    }
    printf("\n");

    return;
}

bool printSuffixTree(char *str, SuffixNode *root) {
    if (root == NULL) {
        cout << "Empty suffix tree!" << endl;
        return false;
    }

    int end_loc = root->cur_pos;
    int start_loc;
    SuffixNode *node;
    node = root->children_first;
    while (node) {
        start_loc = node->start_pos;
        printString(str, start_loc, end_loc);
        node = node->next;
    }

    return true;
}


